<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/23 0023
 * Time: 上午 9:38
 */

namespace app\admin\model;


use think\facade\Cookie;
use think\facade\Session;
use think\Model;
use think\facade\Request;

class UserModel extends Model
{
    protected $table = "user";
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    public function role()
    {
        return $this->belongsToMany("RoleModel","userRoleRelation","role_id","user_id");
    }

    /**
     * 记录登录时间 ip地址
     * @param $id
     * @return bool
     */
    static public function recordIp($id)
    {
        $model = self::get($id);
        $model->last_login_ip = Request::ip();
        $model->last_login_time = time();
        $model->save();
        return true;
    }

    /**
     * 验证用户是否存在 是否被冻结
     * @param $username
     * @return array|string
     */
    static public function userExists($username)
    {
        $model = new self();
        $result = $model->where("username",$username)->find();
        if (isset($result["status"])) {
            return $result["status"] === 0 ? "您的账号被冻结" : $result;
        }
        return $result;
    }
}